﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace VirtualTournaments.Frontend.MenuControls.GamblingManagerMenu.InnerControls
{
    using Team = Services.Tournaments.Team;
    using TeamsServices = Services.Tournaments.TeamsService;
    using BetsService = Services.Bets.BetsService;
    using User = Services.Users.User;
    using Tournament = Services.Tournaments.Tournament;
    using TournamentsService = Services.Tournaments.TournamentsService;
    using TournamentTeamOdds = Services.Bets.TorunamentTeamOdds;

    public partial class TournamentBet : System.Web.UI.UserControl
    {
        public EventHandler GoToTeamInfoButton;
        public EventHandler GoToCancelBetButton;

        public string TournamentName 
        {
            get 
            {
                return TournamentNameLabel.Text;
            }
            set 
            {
                TournamentNameLabel.Text = value;
            }
        }
        
        public ListItemCollection TeamsInTournament 
        {
            get { return TeamsListBox.Items; }
            private set{}
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Team[] teamsInTournament;
            TournamentName = Session["CurrentTournament"] as string;

            if (!TournamentName.Equals(string.Empty))
            {
                teamsInTournament = TeamsServices.GetTeamsByTournamentName(TournamentNameLabel.Text);
                this.TeamsListBox.Items.Clear();

                foreach (Team team in teamsInTournament)
                {
                    if ( (!team.Name.Equals(FirstPlaceTeam.Text))
                        && (!team.Name.Equals(SecondPlaceTeam.Text))
                        && (!team.Name.Equals(ThirdPlaceTeam.Text)))
                    this.TeamsListBox.Items.Add(team.Name);
                }

                TableRow row;
                TableCell teamCell, firtPlaceOddCell, secondPlaceOddCell, thirdPlaceOddCell;

				Tournament currentTournament = TournamentsService.GetTournamentByName(TournamentName);
				TournamentTeamOdds[] tournamentTeamOdds = BetsService.GetTorunamentsTeamOdds(currentTournament);

				//TournamentTeamOdds[] tournamentTeamOdds = GetTorunamentsTeamOdds();

                foreach (TournamentTeamOdds tournamentTeamOdd in tournamentTeamOdds)
                {
                    row = new TableRow();
                    teamCell = new TableCell { Text = tournamentTeamOdd.Team.Name, HorizontalAlign = HorizontalAlign.Center };
                    firtPlaceOddCell = new TableCell { Text = tournamentTeamOdd.OddsForFirst.ToString(), HorizontalAlign = HorizontalAlign.Center };
                    secondPlaceOddCell = new TableCell { Text = tournamentTeamOdd.OddsForSecond.ToString(), HorizontalAlign = HorizontalAlign.Center };
                    thirdPlaceOddCell = new TableCell { Text = tournamentTeamOdd.OddsForThird.ToString(), HorizontalAlign = HorizontalAlign.Center };

                    row.Cells.Add(teamCell);
                    row.Cells.Add(firtPlaceOddCell);
                    row.Cells.Add(secondPlaceOddCell);
                    row.Cells.Add(thirdPlaceOddCell);

                    OddsInfoTable.Rows.Add(row);
                }
            }
        }

        protected void FirstPlaceToButton_OnClick(object sender, EventArgs e)
        {
            if(TeamsListBox.SelectedIndex >= 0)
            {
                if (!FirstPlaceTeam.Text.Equals("Team: "))
                {
                    TeamsListBox.Items.Add(FirstPlaceTeam.Text);
                }
                FirstPlaceTeam.Text = TeamsListBox.SelectedValue;
                TeamsListBox.Items.RemoveAt(TeamsListBox.SelectedIndex);
            }
            
        }
        protected void FirstPlaceFromButton_OnClick(object sender, EventArgs e) 
        {
            if (!FirstPlaceTeam.Text.Equals("Team: "))
            {
                TeamsListBox.Items.Add(FirstPlaceTeam.Text);
                FirstPlaceTeam.Text = "Team: ";
            }
  
        }

        protected void SecondPlaceToButton_OnClick(object sender, EventArgs e)
        {
            if (TeamsListBox.SelectedIndex >= 0)
            {
                if (!SecondPlaceTeam.Text.Equals("Team: "))
                {
                    TeamsListBox.Items.Add(SecondPlaceTeam.Text);
                }
                SecondPlaceTeam.Text = TeamsListBox.SelectedValue;
                TeamsListBox.Items.RemoveAt(TeamsListBox.SelectedIndex);
            }
            
        }
        protected void SecondPlaceFromButton_OnClick(object sender, EventArgs e)
        {
            if (!SecondPlaceTeam.Text.Equals("Team: "))
            {
                TeamsListBox.Items.Add(SecondPlaceTeam.Text);
                SecondPlaceTeam.Text = "Team: ";
            }

        }

        protected void ThirdPlaceToButton_OnClick(object sender, EventArgs e)
        {
            if (TeamsListBox.SelectedIndex >= 0)
            {
                if (!ThirdPlaceTeam.Text.Equals("Team: "))
                {
                    TeamsListBox.Items.Add(ThirdPlaceTeam.Text);
                }
                ThirdPlaceTeam.Text = TeamsListBox.SelectedValue;
                TeamsListBox.Items.RemoveAt(TeamsListBox.SelectedIndex);
            }
        }
        protected void ThirdPlaceFromButton_OnClick(object sender, EventArgs e)
        {
            if (!ThirdPlaceTeam.Text.Equals("Team: "))
            {
                TeamsListBox.Items.Add(ThirdPlaceTeam.Text);
                ThirdPlaceTeam.Text = "Team: ";
            }

        }

        protected void TeamInfoButton_OnClick(object sender, EventArgs e) 
        {
            if (this.TeamsListBox.SelectedIndex >= 0)
                GoToTeamInfoButton(this, new VirtualTournamentsFrontendEvent());
        }

        protected void AcceptBetButton_OnClick(object sender, EventArgs e)
        {
            //TODO: validate amounts!!!
            if ((!FirstPlaceTeam.Text.Equals("Team: ")) &&
                 (!SecondPlaceTeam.Text.Equals("Team: ")) &&
                 (!ThirdPlaceTeam.Text.Equals("Team: ")))
            {
                BetInfoLabel.Text = "Winner:    " + FirstPlaceTeam.Text + "(u$s " + FirstPlaceBetAmountTextBox.Text + ")<br />";
                BetInfoLabel.Text += "Second:   " + SecondPlaceTeam.Text + "(u$s " + SecondPlaceBetAmountTextBox.Text + ")<br />";
                BetInfoLabel.Text += "Third:    " + ThirdPlaceTeam.Text + "(u$s " + ThirdPlaceBetAmountTextBox.Text + ")<br />";
                
                pagedimmer.Visible = true;
                msgbox.Visible = true;
            }
        }
        protected void ResetBetButton_OnClick(object sender, EventArgs e) 
        {
            if (!FirstPlaceTeam.Text.Equals("Team: "))
                TeamsListBox.Items.Add(FirstPlaceTeam.Text);
            if (!SecondPlaceTeam.Text.Equals("Team: "))
                TeamsListBox.Items.Add(SecondPlaceTeam.Text);
            if (!ThirdPlaceTeam.Text.Equals("Team: "))
                TeamsListBox.Items.Add(ThirdPlaceTeam.Text);

            FirstPlaceTeam.Text = SecondPlaceTeam.Text = ThirdPlaceTeam.Text = "Team: ";
            FirstPlaceBetAmountTextBox.Text = SecondPlaceBetAmountTextBox.Text = ThirdPlaceBetAmountTextBox.Text = "";
        }
        protected void CancelBetButton_OnClick(object sender, EventArgs e) 
        {
            GoToCancelBetButton(this, new VirtualTournamentsFrontendEvent());
        }

        protected void TournamentBetGoBackButton_OnClick(object sender, EventArgs e) 
        {
            //TODO: left this way because the effect is the same of CancelBetButton_OnClick          
            GoToCancelBetButton(this, new VirtualTournamentsFrontendEvent());
        }

        protected void FinishCurrentBetButton_OnClick(object sender, EventArgs e)
        {
            pagedimmer.Visible = false;
            msgbox.Visible = false;
            FinishCurrentBetButton.Enabled = false;
            ConfirmCurrentBetButton.Enabled = true;
            CancelCurrentBetButton.Enabled = true;
            StatusLabel.Text = "Waiting for confirmation...";

            //TODO: left this way because the effect is the same of CancelBetButton_OnClick          
            GoToCancelBetButton(this, new VirtualTournamentsFrontendEvent());
        }

        protected void ConfirmCurrentBetButton_OnClick(object sender, EventArgs e)
        {
            //TODO: process bet!
            User user = Session["CurrentUser"] as User;
            Tournament tournament = TournamentsService.GetTournamentByName(TournamentName);
            Team firsPlaceTeam = new Team(FirstPlaceTeam.Text);
            Team secondPlaceTeam = new Team(SecondPlaceTeam.Text);
            Team thirdPlaceTeam = new Team(ThirdPlaceTeam.Text);

            //BetsService.BetTournament(tournament, user, Convert.ToDecimal(FirstPlaceBetAmountTextBox.Text), firsPlaceTeam, VirtualTournaments.Services.Bets.TournamentPositionEnum.First);
            //BetsService.BetTournament(tournament, user, Convert.ToDecimal(SecondPlaceBetAmountTextBox.Text), secondPlaceTeam, VirtualTournaments.Services.Bets.TournamentPositionEnum.Second);
            //BetsService.BetTournament(tournament, user, Convert.ToDecimal(SecondPlaceBetAmountTextBox.Text), thirdPlaceTeam, VirtualTournaments.Services.Bets.TournamentPositionEnum.Third);

            StatusLabel.Text = "Your bet has been registered! Click 'Finish' to complete the operation.";
            FinishCurrentBetButton.Enabled = true;
            ConfirmCurrentBetButton.Enabled = false;
            CancelCurrentBetButton.Enabled = false;
        }

        protected void CancelCurrentBetButton_OnClick(object sender, EventArgs e)
        {
            pagedimmer.Visible = false;
            msgbox.Visible = false;
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            TeamsListBox.Items.Clear();
            FirstPlaceBetAmountTextBox.Text = "";
            SecondPlaceBetAmountTextBox.Text = "";
            ThirdPlaceBetAmountTextBox.Text = "";
            FirstPlaceTeam.Text = "Team: ";
            SecondPlaceTeam.Text = "Team: ";
            ThirdPlaceTeam.Text = "Team: ";
        }

        private TournamentTeamOdds[] GetTorunamentsTeamOdds() 
        {
            List<TournamentTeamOdds> tournamentTeamOddsArray = new List<TournamentTeamOdds>();

            TournamentTeamOdds newOdd1 = new TournamentTeamOdds(new Team("Boca"), 10, 5, 20);
            TournamentTeamOdds newOdd2 = new TournamentTeamOdds(new Team("River"), 1, 6, 0);
            TournamentTeamOdds newOdd3 = new TournamentTeamOdds(new Team("Racing"), 6, 9, 2);
            TournamentTeamOdds newOdd4 = new TournamentTeamOdds(new Team("Velez"), 1, 5, 0);
            TournamentTeamOdds newOdd5 = new TournamentTeamOdds(new Team("Independiente"), 34, 75, 70);
            TournamentTeamOdds newOdd6 = new TournamentTeamOdds(new Team("San Lorenzo"), 3, 4, 80);
            TournamentTeamOdds newOdd7 = new TournamentTeamOdds(new Team("Lanus"), 67, 90, 33);

            tournamentTeamOddsArray.Add(newOdd1);
            tournamentTeamOddsArray.Add(newOdd2);
            tournamentTeamOddsArray.Add(newOdd3);
            tournamentTeamOddsArray.Add(newOdd4);
            tournamentTeamOddsArray.Add(newOdd5);
            tournamentTeamOddsArray.Add(newOdd6);
            tournamentTeamOddsArray.Add(newOdd7);

            return tournamentTeamOddsArray.ToArray();
        }
    }
}